package cn.hou.java.designpattern._03proprtype;

import java.util.ArrayList;
import java.util.List;

//必须实现Cloneable接口才能被克隆
public class Person implements Cloneable {
	
	private String name;
	
	private String age;

	private List<String> friends;
	
	//浅克隆,引用类型不会克隆(List等),会共用同一个地址,一个变,克隆的跟着变
//	protected Person clone() throws CloneNotSupportedException {
//		return (Person) super.clone();
//	}
	//深克隆
	protected Person clone() throws CloneNotSupportedException {
		Person person=new Person();
		List<String> friends2 = this.getFriends();//获取本体list
		//创建一个新的list
		List newList=new ArrayList();
		for (String string : friends2) {//遍历本体的,赋值给克隆后的,即新new的
			newList.add(string);
		}
		person.setFriends(newList);
		return person;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public List<String> getFriends() {
		return friends;
	}

	public void setFriends(List<String> friends) {
		this.friends = friends;
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", friends=" + friends + "]";
	}
	
}
